#include "AdminFoodWindow.h"
#include "ui_AdminFoodWindow.h"

AdminFoodWindow::AdminFoodWindow(QWidget *parent) :
    QListWidget(parent),
    ui(new Ui::AdminFoodWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("管理食物");
    /**
      * @leiteorz
      * 加载食物列表
      */
    FoodTable food_table;
    Food* foodList = new Food[100];
    int i = 0;
    foodList = food_table.queryAllFood();
    while(foodList[i].name != ""){
        /**
          * @leiteorz
          * 绘制ListItem
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(798,89));
        AdminFoodItem *adminFoodItem = new AdminFoodItem();
        adminFoodItem->init(foodList[i]);
        this->setItemWidget(item,adminFoodItem);

        connect(adminFoodItem,SIGNAL(dataChange()),this,SLOT(replyDataChange()));
        connect(adminFoodItem,SIGNAL(dataChange2()),this,SLOT(replyDataChange2()));

        i++;
    }
}

AdminFoodWindow::~AdminFoodWindow()
{
    delete ui;
}

void AdminFoodWindow::replyShowAdminFoodWindow(){
    this->show();
}

void AdminFoodWindow::replyDataChange(){
    //发出信号告诉订单界面dataChange
    emit dataChange();
}


void AdminFoodWindow::replyDataChange2(){
    this->clear();
    /**
      * @leiteorz
      * 加载食物列表
      */
    FoodTable food_table;
    Food* foodList = new Food[100];
    int i = 0;
    foodList = food_table.queryAllFood();
    while(foodList[i].name != ""){
        /**
          * @leiteorz
          * 绘制ListItem
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(798,89));
        AdminFoodItem *adminFoodItem = new AdminFoodItem();
        adminFoodItem->init(foodList[i]);
        this->setItemWidget(item,adminFoodItem);

        connect(adminFoodItem,SIGNAL(dataChange()),this,SLOT(replyDataChange()));
        connect(adminFoodItem,SIGNAL(dataChange2()),this,SLOT(replyDataChange2()));

        i++;
    }
}
